package com.ruoyi.project.module.shao;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.YqgInformationProject;
import com.ruoyi.project.domain.YqgItemAssociation;
import com.ruoyi.project.domain.YqgSetMeal;
import com.ruoyi.project.service.IYqgInformationProjectService;
import com.ruoyi.project.service.IYqgItemAssociationService;
import com.ruoyi.project.service.IYqgSetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 套餐项目关联Controller
 * 
 * @author ruoyi
 * @date 2023-08-15
 */
@RestController
@RequestMapping("/shao/association")
public class ShaoYqgItemAssociationController extends BaseController
{
    @Autowired
    private IYqgItemAssociationService yqgItemAssociationService;
    @Autowired
    private IYqgSetMealService yqgSetMealService;
    @Autowired
    private IYqgInformationProjectService yqgInformationProjectService;

    /**
     * 查询套餐项目关联列表
     */
//    @RequiresPermissions("project:association:list")
    @GetMapping("/list")
    public TableDataInfo list(YqgItemAssociation yqgItemAssociation)
    {
        List<YqgItemAssociation> list = yqgItemAssociationService.selectYqgItemAssociationList(yqgItemAssociation);
        List<YqgInformationProject> plist = new ArrayList<>();
        for(int i =0;i<list.size();i++){
            YqgInformationProject yqgInformationProject=new YqgInformationProject();
            yqgInformationProject=yqgInformationProjectService.selectYqgInformationProjectByID(list.get(i).getProjectId());
            plist.add(yqgInformationProject);
        }
        return getDataTable(plist);
    }

    /**
     * 导出套餐项目关联列表
     */
//    @RequiresPermissions("project:association:export")
    @Log(title = "套餐项目关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqgItemAssociation yqgItemAssociation)
    {
        List<YqgItemAssociation> list = yqgItemAssociationService.selectYqgItemAssociationList(yqgItemAssociation);
        ExcelUtil<YqgItemAssociation> util = new ExcelUtil<YqgItemAssociation>(YqgItemAssociation.class);
        util.exportExcel(response, list, "套餐项目关联数据");
    }

    /**
     * 获取套餐项目关联详细信息
     */
//    @RequiresPermissions("project:association:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yqgItemAssociationService.selectYqgItemAssociationById(id));
    }

    /**
     * 新增套餐项目关联
     */
//    @RequiresPermissions("project:association:add")
    @Log(title = "套餐项目关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YqgItemAssociation yqgItemAssociation)
    {
        YqgSetMeal yqgSetMeal=new YqgSetMeal();
        return toAjax(yqgItemAssociationService.insertYqgItemAssociation(yqgItemAssociation));
    }

    /**
     * 修改套餐项目关联
     */
//    @RequiresPermissions("project:association:edit")
    @Log(title = "套餐项目关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqgItemAssociation yqgItemAssociation)
    {
        return toAjax(yqgItemAssociationService.updateYqgItemAssociation(yqgItemAssociation));
    }

    /**
     * 删除套餐项目关联
     */
//    @RequiresPermissions("project:association:remove")
    @Log(title = "套餐项目关联", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        yqgItemAssociationService.selectYqgItemAssociationById(ids[0]);
        System.out.println(ids[0]);
        return toAjax(yqgItemAssociationService.deleteYqgItemAssociationByIds(ids));
    }
}
